/*
** my_str_to_wordtab.c for mysh in /home/nadali_t//local/svn/tek2ed/minishell1
** 
** Made by thomas nadalie
** Login   <nadali_t@epitech.net>
** 
** Started on  Thu Nov  8 12:23:04 2012 thomas nadalie
** Last update Thu Nov  8 12:24:33 2012 thomas nadalie
*/

#include        <stdlib.h>
#include	"my.h"

int             my_wordlen(char *str, int i)
{
  int           nb;

  nb = 0;
  while (str[i] != ' ' && str[i])
    {
      i++;
      nb++;
    }
  return (nb);
}

int             count_word(char *str)
{
  int           i;
  int           nb;

  i = 0;
  nb = 1;
  while (str[i])
    {
      while (str[i] == ' ')
        i++;
      while (str[i] != ' ' && str[i])
        i++;
      if (str[i])
	nb++;
    }
  return (nb);
}

char		*my_bzero(char *b, size_t size)
{
  int		i;

  i = 0;
  while (i <= size)
    b[i++] = '\0';
  return (b);
}

char            *copy_word(char *str, int i)
{
  char          *new;
  int           j;

  j = 0;
  new = malloc(sizeof(new) * my_wordlen(str, i));
  if (new == NULL)
    exit(1);
  new = my_bzero(new, my_wordlen(str, i));
  while (str[i] && str[i] != ' ' && str[i] != '\n')
    new[j++] = str[i++];
  new[j] = '\0';
  return (new);
}

char            **my_str_to_wordtab(char *str)
{
  char          **tab;
  int           i;
  int           j;

  i = 0;
  j = 0;
  tab = malloc(sizeof(*tab) * (count_word(str) + 2));
  while (str[i])
    {
      while (str[i] == ' ' && str[i])
      	i++;
      tab[j++] = copy_word(str, i);
      while (str[i] != ' ' && str[i])
	i++;
    }
  tab[j] = NULL;
  return (tab);
}
